package com.example.beat_x3.Adapter;

import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.beat_x3.Client.LoadImages;
import com.example.beat_x3.R;

import java.util.ArrayList;

import Entity.Inf;
import Entity.Rental;


public class BorrowAdapter extends RecyclerView.Adapter<BorrowAdapter.Holder> {

    public ArrayList<Rental> borrows;

    private BorrowAdapter.OnItemClickListener mOnItemClickListener;

    public interface OnItemClickListener {
        void onItemClick(String opposite);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    @NonNull
    @Override
    public BorrowAdapter.Holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new BorrowAdapter.Holder(LayoutInflater.from(Inf.context).inflate(R.layout.item_borrow, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull BorrowAdapter.Holder holder, int position) {
        holder.bind(position);
    }

    @Override
    public int getItemCount() {
        if (borrows == null) return 0;
        return borrows.size();
    }

    class Holder extends RecyclerView.ViewHolder {

        TextView lend_user, date, status_text;
        ImageView status, imageView;
        String rentalNumber, opposite;
        boolean st;
        int position;

        public Holder(@NonNull View itemView) {
            super(itemView);

            this.lend_user = itemView.findViewById(R.id.borrow_text2);
            this.date = itemView.findViewById(R.id.borrow_text3);
            this.status = itemView.findViewById(R.id.rentalstatus);
            this.imageView = itemView.findViewById(R.id.borrow_picture);
            this.status_text = itemView.findViewById(R.id.rental_status_text);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mOnItemClickListener != null) {
                        mOnItemClickListener.onItemClick(opposite);
                    }
                }
            });
        }

        void bind(int position) {
            opposite = borrows.get(position).StudentLend;
            rentalNumber = borrows.get(position).RentalNumber;
            lend_user.setText("出租用户：" + opposite);
            date.setText("截止日期：" + borrows.get(position).BorrowDate);
            this.position = position;
            int n = borrows.get(position).RentalStatus;
            st = n == 1 || n == 2;
            switch (n) {
                case 0:
                    status.setImageResource(R.drawable.red);
                    status_text.setText(R.string.borrowing);
                    break;
                case 1:
                    status.setImageResource(R.drawable.green);
                    status_text.setText(R.string.returned);
                    break;
                case 2:
                    status.setImageResource(R.drawable.cancel);
                    status_text.setText(R.string.lost);
                    break;
            }
            if (Inf.equipment_images != null) {
                if (!Inf.equipment_images.containsKey(borrows.get(position).EquipmentImage)) {
                    LoadImages.downloadImages(LoadImages.EQUIPMENT_SRC, borrows.get(position).EquipmentImage);
                }
                imageView.setImageURI(Uri.parse(LoadImages.EQUIPMENT_SRC + "/" + borrows.get(position).EquipmentImage));
            }
        }
    }
}
